import { Component, OnInit, ViewChild } from '@angular/core';
import { PageModel } from "../../../../model/page-model";
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';

@Component({
  selector: 'devices',
  templateUrl: './devices.component.html',
  styleUrls: ['./devices.component.css'],
  animations: [
    flyIn
  ]

})
export class Devices implements OnInit {
  devData: any[];
  @ViewChild('myModal') public myModal: ModalDirective;
  public pageModel = new PageModel();
  private page: number = 1;
  public keyWord: string = '';
  private key_word: string = '';
  public disabled = '';
  devicesNum: number = 0;
  locker_category: string = '';
  locker_module_number: string = '';
  locker_channel_trader: any;
  locker_imei_number: string = '';
  locker_longitude: string = '';
  locker_latitude: string = '';
  btype: number;
  selectedId: any;
  productdata: any;
  product_id: any = '';

  constructor(private myHttp: MyHttp) { }
  ngOnInit() {
    this.key_word = this.keyWord = localStorage.getItem('product_device_key_word')?localStorage.getItem('product_device_key_word'):'';
    this.getdata(1);
    this.getproductdata();
  }

  getdata(page) {
    this.page = page;
    this.myHttp.post('locker/findAll?page=' + this.page + '&key_word=' + this.key_word, {}).subscribe(res => {
      if (res.errorCode == 0) {       
        this.devData = res.message.data;
        this.devicesNum = res.message.total;
        this.pageModel.setPage({ iTotalRecords: res.message.total });
      }

    })
  }

  savedata() {
    this.disabled = 'disabled';
    this.myHttp.post('gizwits/locker/create',
      {
        "locker_category": this.product_id,
        "module_number": this.locker_module_number,
        "imei_number": this.locker_imei_number,
        "locker_channel_trader": this.locker_channel_trader || '',
        "product_id": this.product_id,
        "locker_map": this.locker_longitude + ',' + this.locker_latitude
      }).subscribe(res => {
        alert(res.message);
        if (res.errorCode == 0) {
          this.cancel();
          this.getdata(this.page);
        }
        this.disabled = '';
      }, err => { this.disabled = ''; });
  }

  deleteModal(item) {
    this.btype = 2;
    this.selectedId = item;
    this.myModal.show();
  }
  getproductdata() {
    this.myHttp.post('product/findAll',
      {

      }).subscribe(res => {
        if (res.errorCode == 0) {
          this.productdata = res.message;
        }
      });
  }

  deldata() {
    this.myHttp.post('gizwits/locker/delete',
      {
        "locker_module_number": this.selectedId
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.myModal.hide();
          this.getdata(this.page);
        }
      });
  }
  search() {
    localStorage.setItem('product_device_key_word',this.keyWord);
    this.key_word = this.keyWord;
    this.getdata(1);
    this.pageModel.currentPage = 1;
  }

  cancel() {
    this.myModal.hide();
    this.product_id = '';
    this.locker_latitude = '';
    this.locker_longitude = '';
    this.locker_category = '';
    this.locker_module_number = '';
    this.locker_imei_number = '';
    this.locker_channel_trader = '';
  }



}

